package org.code.boot.controller.common;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.code.boot.common.annotation.Log;
import org.code.boot.common.enums.BusinessType;
import org.code.boot.common.utils.poi.ExcelUtil;
import org.code.boot.controller.BaseController;
import org.code.boot.vo.AjaxResult;
import org.code.boot.vo.TableDataInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.code.boot.domain.common.ComFavorite;
import org.code.boot.service.common.IComFavoriteService;

/**
 * 通用收藏Controller
 * 
 * @author ruoyi
 * @date 2023-08-27
 */
@RestController
@RequestMapping("/common/favorite")
public class ComFavoriteController extends BaseController
{
    @Autowired
    private IComFavoriteService bizFavoriteService;

    /**
     * 查询通用收藏列表
     */
    @PreAuthorize("@ss.hasPermi('common:favorite:list')")
    @GetMapping("/list")
    public TableDataInfo list(ComFavorite comFavorite)
    {
        startPage();
        List<ComFavorite> list = bizFavoriteService.selectBizFavoriteList(comFavorite);
        return getDataTable(list);
    }

    /**
     * 导出通用收藏列表
     */
    @PreAuthorize("@ss.hasPermi('common:favorite:export')")
    @Log(title = "通用收藏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ComFavorite comFavorite)
    {
        List<ComFavorite> list = bizFavoriteService.selectBizFavoriteList(comFavorite);
        ExcelUtil<ComFavorite> util = new ExcelUtil<ComFavorite>(ComFavorite.class);
        util.exportExcel(response, list, "通用收藏数据");
    }

    /**
     * 获取通用收藏详细信息
     */
    @PreAuthorize("@ss.hasPermi('common:favorite:query')")
    @GetMapping(value = "/{favoriteId}")
    public AjaxResult getInfo(@PathVariable("favoriteId") Long favoriteId)
    {
        return success(bizFavoriteService.selectBizFavoriteByFavoriteId(favoriteId));
    }

    /**
     * 新增通用收藏
     */
    @PreAuthorize("@ss.hasPermi('common:favorite:add')")
    @Log(title = "通用收藏", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ComFavorite comFavorite)
    {
        return toAjax(bizFavoriteService.insertBizFavorite(comFavorite));
    }

    /**
     * 修改通用收藏
     */
    @PreAuthorize("@ss.hasPermi('common:favorite:edit')")
    @Log(title = "通用收藏", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ComFavorite comFavorite)
    {
        return toAjax(bizFavoriteService.updateBizFavorite(comFavorite));
    }

    /**
     * 删除通用收藏
     */
    @PreAuthorize("@ss.hasPermi('common:favorite:remove')")
    @Log(title = "通用收藏", businessType = BusinessType.DELETE)
	@DeleteMapping("/{favoriteIds}")
    public AjaxResult remove(@PathVariable Long[] favoriteIds)
    {
        return toAjax(bizFavoriteService.deleteBizFavoriteByFavoriteIds(favoriteIds));
    }

    /**
     * 收藏/取消收藏
     */
    @PostMapping("/toggle/{businessType}/{businessId}")
    public AjaxResult toggleFavorite(@PathVariable String businessType, 
                                   @PathVariable Long businessId,
                                   @RequestParam(required = false) String folderName)
    {
        return bizFavoriteService.toggleFavorite(businessType, businessId, folderName);
    }

    /**
     * 获取收藏信息
     */
    @GetMapping("/info/{businessType}/{businessId}")
    public AjaxResult getFavoriteInfo(@PathVariable String businessType, @PathVariable Long businessId)
    {
        return bizFavoriteService.getFavoriteInfo(businessType, businessId);
    }
}